/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-05-29 08:50:26
 * @LastEditors: voanit
 * @LastEditTime: 2022-05-29 16:02:42
 */

import { TodoAction } from '../actions/todo'
type TodoType = {
  id: number
  name: string
  isDone: boolean
}

const initValue: TodoType[] = [
  {
    id: 1,
    name: 'title',
    isDone: true,
  },
]
const getTodo = (state = initValue, action: TodoAction) => {
  switch (action.type) {
    case 'INIT_TODO':
      return action.data
    case 'ADD_TODO':
      return [
        ...state,
        {
          id: Date.now(),
          name: action.name,
          isDone: true,
        },
      ]
    case 'DEL_TODO':
      return state.filter((item) => item.id !== action.id)
    case 'CHANGE_STATE_TODO':
      return state.map((item) => {
        if (item.id === action.payload.id) {
          return {
            ...item,
            isDone: action.payload.newIsDone,
          }
        } else {
          return { ...item }
        }
      })

    default:
      return state
  }
}
export default getTodo
